import { GrowlMessageType, MessageBoxService } from './../../components/messageBox';
import { StoreSvr } from './../../services/store';
import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'display-setup',
    styleUrls: ['./index.scss'],
    template: `
        <div class="displaysetup-container">
            <header class="title">切换厨显方案</header>
            <section class="content">
                <p>如果厨房有多个档口制作不同分类的菜品，可以在店铺管理“设置--厨房打印显示方案”添加或修改厨房显示方案。</p>
                <p>
                    <md-radio-group class="radio-group" [ngModel]="selectedKitchenSolutionId" (ngModelChange)=change($event) >
                        <md-radio-button class="radio-button" *ngFor="let kitchenSolution of kitchenSolutions"  [value]="kitchenSolution.id">
                            {{kitchenSolution.name}}
                        </md-radio-button>
                    </md-radio-group>
                </p>
            </section>
        </div>
    `
})
export class DisplaySetup implements OnInit {
    constructor(private storeSvr: StoreSvr) { }
    kitchenSolutions: any[];
    selectedKitchenSolutionId: any;
    private selectKitchenSolution: any;
    async ngOnInit() {
        this.kitchenSolutions = await this.storeSvr.getKdsKitchens();
        this.selectedKitchenSolutionId = this.storeSvr.currentDevice.kitchenSolutionId;
        this.change(this.selectedKitchenSolutionId);
    }

    async change(id) {
        try {
            this.selectKitchenSolution = this.kitchenSolutions.find(x => x.id == id);
            const device = await this.storeSvr.changeDeviceKitchenSolution(this.storeSvr.getCurrentDeviceId(), this.selectKitchenSolution.id);
            this.storeSvr.currentDevice = device;
            const kitchensolutions = await this.storeSvr.getDeviceKitchensolutions(this.storeSvr.getCurrentDeviceId());
            this.storeSvr.currentKitchenSolutions = kitchensolutions.reverse();
            MessageBoxService.current.refreshKitchenSolutionName();
        } catch (error) {
            MessageBoxService.current.Growl(GrowlMessageType.Error, "错误提示", error.message || error);
        }
    }
}